/*
 * 多线程编程，线程同步
 * 
 * java.lang.Thread
 * java.lang.Runnable public void run();
 */

import java.net.Socket;

public class multithreading{

    public void Task1(){

    }

    public void Task2(){

    }
    public static void main(String[] args) {
        Thread t1 = new Thread(new Task1());
        Thread t2 = new Thread(new Task2());   

        t1.start();
        t2.start();

        t2.stop();

        t1.suspend();// 挂起

        t1.resume();// 重新申请执行

        Thread.sleep(11); //大于等于10ms
        //synchronized
    }


    public class MyTask implements Runnable{
        public void run(){

        }
        public static void main(String[] args) {
            Runnable r = new MyTask();
            Thread t = new Thread();
            t.start();
            new Thread(new MyTask()).start();
            
        }
    }

    public class MyThread extends Thread{
        @Override
        public void run(){
            ...
        }
    }

    void a(){
    new Thread(){ public void run(){ }}.start();

    new Thread(new Runnable() {
        public void run(){}
    }).start();

    //第三种 函数式编程
    new Thread(() -> {}).start();
    }

 }

 // 多线程的同步问题

public synchronized void push(char c){  //  synchronized    同步对象锁
    data[index] = c;
    index++;
}

class A{
    int i,j;
    int index;
    int[] data;
    double x,y;
    Object o = new byte[1000];
    Object lockB = new byte[0];
    public void a(){
        int a = 0;
        int b = 9;
        synchronized (lockB) {
            //access i,j
        }
    }

    public void b(){
        synchronized (lockB) {
            //access i,j
        }
    }

    public void c(){
        synchronized (o) {
            //access x,y
        }
        
    }

    public void d(){
        synchronized (o) {
            //access x,y
        }
        
    }

    synchronized public int pop(){
        // if(index == 0){
        //     while (true) { 
        //         Thread.sleep(11);
        //         }
        // }else{
        //     index--;
        //     return data[index];
        // }

        //  Object,wait,notify,notifyALL

        //  线程阻塞
        while(index == 0){//    不应该使用if来判断
            try { //    对阻塞条件的判断应该使用while
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        index--;
        notifyAll(); // 这样比调用notify()更好 唤醒
        return data[index];
    }
}

Socket socket = new Socket(host,port);

// 线程池


public synchronized void run(){

    
}
// Socket       ServerSocket





















































































